<?php
namespace Home\Controller;
use Home\Aip\User;
use Home\Aip\Store;
use Home\Aip\StoreClass;

class ShopController extends CommonController {	
	protected $table = 'product';
	protected $st_class = 'store_class';
	protected $st_grade = 'store_grade';
	protected $gc_class = 'goods_class';
	protected $st_store = 'store';
	protected $st_join = 'store_joinin';


	
	/*
	*省市ajax
	*/	
    public function code_chage(){
        if(IS_POST){
			if ($_POST['id']) {
				if (!$_POST['parent']) {
					$list = code_area_son('area_parent_id',$_POST['id']);
				} else {
					$lists = code_area_son('area_parent_id',$_POST['id']);
					$list = code_area_son('area_parent_id',$lists[0]['area_id']);	
				}				
				$this->ajaxReturn($list);
				//echo json_decode($data);
			}			
        }
    } 
	
	/*
	*商品分类ajax
	*/	
    public function g_class_chage(){
        if(IS_POST){
			if (I('post.type')=='gc_class') {
				if (I('post.id')) {					
					$list = goods_class_son('gc_parent_id',I('post.id'));
				}			
				$this->ajaxReturn($list);
				//echo json_decode($data);
			}			
        }
    } 
	
	/*
	 * 判断是否已经入驻
	 */
	public function is_state() {
		if(!is_null($_SESSION['shop_store'])) {
			$result = M('store') -> field('store_state') -> where('store_id = %d', $_SESSION['shop_store']['store_id']) -> find();
			if($result['store_state'] == 1) {
				$this->redirect('SpStore/index');
			}
		}
		return false;
	}

	/*
	*商家入驻首页
	*/		
    public function sp_storein(){
		$this->is_state();
		$this->display();
    }

	/*
	*商家申请协议
	*/	
    public function sp_sep0(){
		$this->is_state();		
		$this->display();
    }
	
	/*
	*企业入驻
	*/		
    public function sp_sep1_b2c(){		
		$this->display();
    }
	
	/*
	*个人入驻
	*/		
    public function sp_sep1_c2c(){		
		$this->display();
    } 
	/*
	*个人入驻
	*填写个人资料
	*/		
    public function sp_sep2_c2c(){
			//dump($_SESSION['store_data']);die;		
		if (IS_POST) {
			//提交hash防止数据重复提交
			$hash = shopHash('one_store_injon',I('post.sphash'));

			if ($hash) {
				$store_data ['company_name'] = I('post.shop_name');//企业名称
				if (I('post.shop_province')) {
					$store_data ['company_province'] = I('post.shop_province');//省
					$store_data ['company_city'] = I('post.shop_city');//市
					$store_data ['company_area'] = I('post.shop_area');	//区			
				}
				$store_data ['company_address'] = I('post.shop_address');//所在地
				$store_data ['company_address_detail'] = I('post.shop_address_detail');//详细地址
				$store_data ['contacts_name'] = I('post.link_name');//联系人姓名
				$store_data ['contacts_phone'] = I('post.link_phone');//联系人电话
				$store_data ['contacts_email'] = I('post.link_email');//电子邮箱
				$store_data ['business_sphere'] = I('post.one_sphere');//身份证姓名
				$store_data ['business_licence_number'] = I('post.one_licence_number');//身份证号
				$store_data ['organization_code_electronic'] = upload();//身份证照片
				
			} else {
				$store_data = false;
			}

			if ($store_data) {				
				session('store_data',$store_data);
				unset($store_data);
			}			
		}
		$this->display();
    }

	/*
	*个人入驻
	*财务资质信息
	*店铺经营信息
	*/		
    public function sp_sep3_c2c(){
		if (IS_POST) {			
				$store_data ['settlement_bank_account_name'] = I('post.set_bank_name');//开户姓名
				$store_data ['settlement_bank_account_number'] = I('post.set_bank_number');//银行账号
				
			if ($store_data) {
				$_SESSION['store_data']['settlement_bank_account_name'] = $store_data ['settlement_bank_account_name'] ;	
				$_SESSION['store_data']['settlement_bank_account_number'] = $store_data ['settlement_bank_account_name'] ;				
				unset($store_data);
			}			
		}		

		//店铺分类
/* 		$str1 = "sc_id>%d and gc_parent_id=%d";
		$data1 = array(0,0);	
		$class = _getList($this->st_class,$str1,$data1,'','sc_id'); */

		unset($join);
		unset($field);		
		unset($order);
		unset($str);
		unset($data);
		unset($table);
		$order = 'sc.sc_id desc';		
		$table = '__STORE_CLASS__ as sc ' ;
		$join ['join1'] = ' INNER  JOIN __GOODS_CLASS__ as gc ON gc.gc_name=sc.sc_name ' ;
		$field = 'sc.*' ;	
		$str = "sc.sc_id>%d and gc.gc_parent_id=%d";
		$data = array(0,0);
			
		$class = _RowsJoin($table,$join,$str,$data,$field,$order) ;
		
		//dump($class);die;
		//店铺等级
		$str2 = "sg_id>%d";
		$data2 = array(0);	
		$grade = _getList($this->st_grade,$str2,$data2,'','sg_id');	
/* 		//经营类目
		$str3 = "gc_id>%d and gc_parent_id=%d";
		$data3 = array(0,0);	
		$gc_class = _getList($this->gc_class,$str3,$data3,'','gc_id');	 */
		
		$this->assign('st_class',$class);
		$this->assign('st_grade',$grade);
		//$this->assign('gc_class',$gc_class);
		$this->display();
    }

	/*
	*个人入驻
	*提交审核
	*/		
    public function sp_sep4_c2c(){
		
		if (IS_POST) {			
				$store_data ['seller_name'] = $_SESSION['user_name'];//商家账号
				$store_data ['store_name'] = I('post.store_name');//店铺名称
				$store_data ['sg_id'] = I('post.sg_id');//店铺等级
				//sg_info 店铺等级收费标准
				unset($str);
				unset($data);
				$str = "sg_id=%d";
				$data = array(I('post.sg_id')) ;
				$grade_one = _getOne($this->st_grade,$str,$data) ; 
				$grade_one['sg_price']?$store_data ['sg_info'] = $grade_one['sg_price'] :$store_data ['sg_info'] = '0';
				
				$store_data ['sg_name'] = I('post.sg_name');//店铺等级名称
				$store_data ['joinin_year'] = I('post.joinin_year');//开店时长
				$store_data ['sc_id'] = I('post.sc_id');//店铺分类
				//sc_name 店铺分类名
				unset($str);
				unset($data);
				$str = "sc_id=%d";
				$data = array(I('post.sc_id')) ;
				$class_one = _getOne($this->st_class,$str,$data) ; 
				$store_data ['sc_name'] = $class_one['sc_name'];				
				$store_data ['sc_bail'] = $class_one['sc_bail'];//店铺分类保证金
				$store_data ['store_class'] = I('post.store_class');//经营类目数量
				$store_data ['store_class_ids'] = strColation(I('post.store_class_ids'));//经营类目ID
				$store_data ['paying_amount'] = $store_data ['joinin_year'] * $store_data ['sg_info'];//应付金额
				//store_class_commis_rates 分类佣金比例
				foreach (I('post.store_class_ids') as $key) {
					$key = substr($key,0,-1);
					//$store_id = $key;
					$key = explode(',',$key);
					$i = count($key) - 1 ;
					
					unset($str);
					unset($data);
					$str = "gc_id=%d";
					$data = array($key[$i]) ;
					$store_bail = _getOne($this->gc_class,$str,$data) ; 
					$commis_rates[] = $store_bail['commis_rate'];					
				}
				$store_data ['store_class_commis_rates'] = strColation($commis_rates);//分类佣金比例				
				$store_data ['store_class_names'] = strColation(I('post.store_class_names'));//经营类目名称
				//dump($store_data);die;
				$store_data ['shop_area_ids'] = serialize(I('post.shop_area_ids'));//发布区域
				
			if ($store_data) {
				
				$_SESSION['store_data']['seller_name'] = $store_data ['seller_name'] ;	
				$_SESSION['store_data']['store_name'] = $store_data ['store_name'] ;
				$_SESSION['store_data']['sg_id'] = $store_data ['sg_id'] ;	
				$_SESSION['store_data']['sg_info'] = $store_data ['sg_info'] ;
				$_SESSION['store_data']['sg_name'] = $store_data ['sg_name'] ;
				$_SESSION['store_data']['joinin_year'] = $store_data ['joinin_year'] ;	
				$_SESSION['store_data']['sc_id'] = $store_data ['sc_id'] ;
				$_SESSION['store_data']['sc_name'] = $store_data ['sc_name'] ;
				$_SESSION['store_data']['sc_bail'] = $store_data ['sc_bail'] ;
				$_SESSION['store_data']['store_class'] = $store_data ['store_class'] ;	
				$_SESSION['store_data']['store_class_ids'] = $store_data ['store_class_ids'] ;
				$_SESSION['store_data']['store_class_commis_rates'] = $store_data ['store_class_commis_rates'] ;
				$_SESSION['store_data']['store_class_names'] = $store_data ['store_class_names'] ;
				$_SESSION['store_data']['member_id'] = $_SESSION['user_id'] ;
				$_SESSION['store_data']['member_name'] = $_SESSION['user_name'] ;
				$_SESSION['store_data']['paying_amount'] = $store_data ['paying_amount'] ;	
				$_SESSION['store_data']['shop_area_ids'] = $store_data ['shop_area_ids'] ;				

				$msg = array('店铺名称','','','','所在地','详细地址','联系人姓名','联系人电话','电子邮箱','身份证姓名','身份证号','身份证照片','支付宝姓名','支付宝账号','','店铺名称','店铺等级','店铺等级','店铺等级','店铺等级名称','开店时长','店铺分类','店铺分类','店铺分类','经营类目','经营类目','经营类目','经营类目','会员登录','会员登录','') ;
				//dump(count($_SESSION['store_data']));
				
				$table = $this->st_join ;
				
				unset($str);
				unset($data);	
				$str = "member_id=%d or member_name='%s'";
				$data = array($_SESSION['user_id'],$_SESSION['user_name']) ;
				$store_ff = _getOne($table,$str,$data) ; 
				
				if ($store_ff) {
					$this->error('您已申请卖家账号，请勿重复申请！','/store_joinin/c2c_step3');
				}
				
				//提交数据
				$join = new Store();
				
				$ADD = $join -> _dataAdd($table,$_SESSION['store_data'],$msg,'store_data');		
				if (in_array($ADD, $msg)) {
					$err = $ADD.'不能为空';
					$this->error($err,'/store_joinin/c2c_step3');
				} else {
					$this->error('提交成功，请等待平台审核！','/store_joinin/c2c_step4');
				}
				unset($store_data);
			}			
		} else {
			unset($str);
			unset($data);
			$str = "member_id=%d or member_name='%s'";
			$data = array($_SESSION['user_id'],$_SESSION['user_name']) ;
			$store_ff = _getOne($this->st_join,$str,$data) ; 
			unset($str);
			unset($data);
			$str = "member_id=%d or member_name='%s'";
			$data = array($_SESSION['user_id'],$_SESSION['user_name']) ;				
			$store_ff2 = _getOne($this->st_store,$str,$data) ; 
				
			if (!$store_ff && !$store_ff2) {
				//未申请
				$this->redirect('/store_joinin/c2c_step1');
			} else {
				if (!$store_ff2) {
					//申请状态 10-已提交申请 11-缴费完成  20-审核成功 30-审核失败 31-缴费审核失败 40-审核通过开店
					if ($store_ff['joinin_state'] == '10') {
						$massege = '入驻申请已经提交，请等待管理员审核';
					} else if ($store_ff['joinin_state'] == '11') {
						$massege = '缴费完成，请等待管理员确认';
					} else if ($store_ff['joinin_state'] == '20') {
						$massege = '资料审核成功，请及时缴费';
					} else if ($store_ff['joinin_state'] == '30') {
						$massege = '审核失败，'.$store_ff['joinin_message'];
					} else if ($store_ff['joinin_state'] == '31') {
						$massege = '缴费审核失败，'.$store_ff['joinin_message'];
					} else if ($store_ff['joinin_state'] == '40') {
						$massege = '审核通过开店';
					}					
				}
				$stc = new Store();	
						
				//dump($store_ff);die;
				$this->assign('joinin',$store_ff);
				//$this->assign('store_class',$store_class);				
				$this->assign('massege',$massege);
			}						
		}
		
		$this->display();
    }

	/*
	*个人入驻
	*/		
    public function sp_sep5_c2c(){	
		
		if (IS_POST) {
			$table = $this->st_join ;
			$type = I('post.state');			
			switch ($type) {
				case '10';
					$error = 1;
					$res = 1 ;
				break;
				case '11';
					$res = 2 ;
					$state = '11' ;
				break;
				case '20';
					$res = 3 ;
					$state = '11' ;
				break;
				case '30';
					$error = 1;
					$res = 4 ;
				break;
				case '31';
					$res = 5 ;
					$state = '11' ;
				break;
				case '40';
					$error = 1;
					$res = 6 ;
				break;
			}
			
			if ($error || !$res) {
				$this->error('非法操作','/store_joinin/c2c_step5');
			}
			
			if ($state && $_SESSION ['user_id']) {
				unset($str);
				unset($data);
				unset($str2);
				unset($data2);				
				$str = "member_id=%d and member_name='%s' and joinin_state=%d";
				$data = array(intval($_SESSION['user_id']),$_SESSION['user_name'],40) ;
				
				$store_f = _getOne($table,$str,$data) ; 	
								
				if ($store_f) {
					$this->error('您已拥有卖家账号，请勿重复申请！','/store_joinin/c2c_step5');
				}
				
				$str2 = "member_id=%d and member_name='%s' and joinin_state=%d";
				$data2 = array($_SESSION['user_id'],$_SESSION['user_name'],$type) ;
				$result ['joinin_state'] = $state;
				$result ['paying_money_certificate'] = upload();//付款凭证
				I('post.paying_money_explain') && $result ['paying_money_certificate_explain'] = I('post.paying_money_explain');
				//dump($type);die;
				//付款凭证说明				
				$result['joinin_state'] && $edit = _getEdit($table,$str2,$data2,$result);
				
				if ($edit) {
					session('st_joinin_data',null);
					unset($result);
					unset($edit);
					$this->success('提交成功！','/store_joinin/c2c_step5');
				} else {
					$this->error('提交失败！','/store_joinin/c2c_step5');
				}
			}	
		} else {
			
			$where ['member_id'] = $_SESSION['user_id'];
			$find_one = _getOne($this->st_join,$where);
			
			$stc = new Store();				
			$this->display();
		}		
		
    } 
	
	/**
	*检测账号
	*/	
    public function checkname(){

		if (I('get.seller_name')) {
			$where ="seller_name='%s'";
			$data  = array(I('get.seller_name'));
			$table = $this->st_store;
		} elseif (I('get.store_name')) {
			$where ="store_name='%s'";
			$data  = array(I('get.store_name'));
			$table = $this->st_store;			
		}
		if ($data) {
			$find = _getOne($table,$where,$data);
		}
				
		unset($data);
		unset($where);
		
		if ($find) {
			unset($find);
			if (I('get.store_name')) {
				echo 'false';
			} else {
				$this->ajaxReturn(array('info'=>'false'));
			}			
		} else {
			unset($find);
			if (I('get.store_name')) {
				echo 'true';
			} else {
				$this->ajaxReturn(array('info'=>'true'));
			}	
		}
    }

	/*
	*数据删除
	*$type=1为店铺分类表 $type=2为店铺等级表 $type=3为店铺表 $type=4为店铺申请表 
	*/
    public function del(){
	
		if (IS_POST) {

			$error = 0 ;	
			$type = I('post.table');
			switch ($type) {
				case '1':
				//$table = $this->st_class;
				break;
				case '2':
				//$table = $this->st_grade;
				break;
				case '3':
				//$table = $this->table;
				break;				
				case '4':
				$table = $this->st_join;
				unset($cache);
				$cache = 'store_joinin'.$_SESSION['user_id'];
				break;
			}
			if ($table) {
				if (I('post.state')=='30') {
					$str = "member_id=%d and member_name='%s' and joinin_state=%d";
					$data = array(I('post.id'),$_SESSION['user_name'],I('post.state'));	
				}
			   	
			}
			if ($data && $str) {			
				 $del = _getDel($table,$str,$data,$cache);	
			}
			unset($str);	
			unset($data);					
			if ($del) {	
				$error = 1 ;			
				$this->ajaxReturn(array('info'=>$error));				
			} else {
				$this->ajaxReturn(array('info'=>$error));	
			}			
		}
    }


 
}